<?php

namespace App\Model;

use App\Util\AliyunUtil;
use Illuminate\Database\Eloquent\Model;

/**
 * 自动打招呼模型
 */
class UserAutoMsg extends Model
{
    protected $table = 'user_auto_msg';

    //删除
    const STATUS_DEL = 0;
    //正常
    const STATUS_OK = 1;
    //待审核
    const STATUS_ING = -1;
    //审核不通过
    const STATUS_FAIL = 2;

    const STATUS = [
        self::STATUS_ING => '待审核',
        self::STATUS_OK => '正常',
        self::STATUS_DEL => '已删除',
        self::STATUS_FAIL => '审核不通过',
    ];

    const TYPE_HOST = [
        'voice' => AliyunUtil::FILE_HOST,
        'video' => AliyunUtil::FILE_HOST,
        'image' => AliyunUtil::IMG_HOST,
        'text' => ''
    ];

    const TYPE_DESC = [
        'voice' => '语音',
        'video' => '视频',
        'image' => '图片',
        'text' => '文字'
    ];

    protected $fillable = [
        'user_id', 'flag', 'msg_type', 'msg_content', 'duration'
    ];

    public function userProfile()
    {
        return $this->belongsTo(UserProfile::class, 'user_id', 'user_id');
    }

    public function personalProfile()
    {
        return $this->belongsTo(UserProfilePersonal::class, 'user_id', 'user_id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function avatarVerf()
    {
        return $this->belongsTo(ProfileVerifyLog::class, 'user_id', 'user_id')
            ->where(['content_type' => ProfileVerifyLog::TYPE_REAL_AVATAR])
            ->where(['status' => ProfileVerifyLog::STATUS_OK])
            ->orderBy('id', 'desc')->limit(1);
    }

    static function veryfyOK($data = null)
    {
        if ($data->flag == self::STATUS_OK) return -1;
        return self::where('id', $data->id)->update(['flag' => self::STATUS_OK]);
    }

    static function veryfyBad($data = null)
    {
        if ($data->flag == self::STATUS_FAIL) return -1;
        $ret = self::where('id', $data->id)->update(['flag' => self::STATUS_FAIL]);
        // $pubTime = (string)$data->created_at;
        $systemMsg = "你的自动打招呼审核不通过，请重新设置你的打招呼语。
1、打招呼语过于单一、简单乏味
2、打招呼图片、不是真人，使用网图
3、打招呼小视频带有其他外部联系方式、ID
4、打招呼语尺度大严重涉黄";
        Message::pushSystem($data->user_id, $systemMsg);
        return $ret;
    }
}
